<?php
/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-09-20
 * 版    本：1.0.0
 * 功能说明：文章控制器。
 *
 **/

namespace Qwadmin\Controller;
use Think\Page;
use Vendor\Tree;

class TodayRecommendController extends ComController
{

    public function add($p=1)
    {
		 //分页
        $p = intval($p) > 0 ? $p : 1;
        $keyword = isset($_GET['keyword']) ? ($_GET['keyword']) : '';
		//$where['other_class_id'] = 4;
        $pagesize = 15;
        $offset = $pagesize * ($p - 1);//计算记录偏移量

        $prefix = C('DB_PREFIX');
        if ($keyword) {
			$where['name'] = array('like','%'.$keyword.'%');
        }
        $novel = D('novel')->where($where)->limit($offset . ',' . $pagesize)->select();

        $count = D('novel')->where($where)->count();
		
        foreach($novel as $key=>$v){
            $nid = $v['id'];
            $novel[$key]['total'] = D('novel_chapter')->where("novel_id = $nid")->count();
        }
	
        $page = new Page($count,$pagesize);
        $page = $page->show();
        $this->assign('page', $page);
        $this->assign('novel',$novel);
        $this->display('form');
    }

    public function index($sid = 0, $p = 1)
    {
        $p = intval($p) > 0 ? $p : 1;
        $recommend = M('today_recommend');
        $pagesize = 20;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $prefix = C('DB_PREFIX');
        $keyword = isset($_GET['keyword']) ? ($_GET['keyword']) : '';
        $order = isset($_GET['order']) ? $_GET['order'] : 'DESC';
        $where = '1 = 1 ';

        $list = D('today_recommend')->where("novel_name like '%$keyword%'")->select();

        if ($keyword) {
            $where .= "and {$prefix}today_recommend.novel_name like '%{$keyword}%' ";
        }
        //默认按照增加顺序降序
        $orderby = "id desc";
        if ($order == "asc") {
            $orderby = "id asc";
        }
        $count = $recommend->where($where)->count();
        $list = $recommend->where($where)->order($orderby)->limit($offset . ',' . $pagesize)->select();
       // echo D()->getLastSql();die;
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->display();
    }

    public function del()
    {

        $ids = isset($_REQUEST['ids']) ? $_REQUEST['ids'] : false;
        if ($ids) {
            if (is_array($ids)) {
                $ids = implode(',', $ids);
                $map['id'] = array('in', $ids);
            } else {
                $map = 'id=' . $ids;
            }
            if (M('today_recommend')->where($map)->delete()) {
                addlog('删除推荐，AID：' . $ids);
                $this->success('恭喜，删除成功！');
            } else {
                $this->error('参数错误！');
            }
        } else {
            $this->error('参数错误！');
        }

    }

    public function edit($id,$p=1)
    {
		//分页
        $p = intval($p) > 0 ? $p : 1;
        $keyword = isset($_GET['keyword']) ? ($_GET['keyword']) : '';
		//$where['other_class_id'] = 4;
        $pagesize = 15;
        $offset = $pagesize * ($p - 1);//计算记录偏移量

        $prefix = C('DB_PREFIX');
        if ($keyword) {
			$where['name'] = array('like','%'.$keyword.'%');
        }
        $novel = D('novel')->where($where)->limit($offset . ',' . $pagesize)->select();

        $count = D('novel')->where($where)->count();
		
        foreach($novel as $key=>$v){
            $nid = $v['id'];
            $novel[$key]['total'] = D('novel_chapter')->where("novel_id = $nid")->count();
        }
	
        $page = new Page($count,$pagesize);
        $page = $page->show();
        $this->assign('page', $page);
        $this->assign('novel',$novel);

        $id = intval($id);
        $recommend = M('today_recommend')->where('id=' . $id)->find();
        if($recommend){
            $this->assign('recommend',$recommend);
        }
        $this->display('form');
    }

    public function update($id = 0)
    {

        $id = intval($id);
        $data['novel_name'] = isset($_POST['novel_name']) ? $_POST['novel_name'] : false;
        $data['chapter_name'] = isset($_POST['chapter_name']) ? $_POST['chapter_name'] : false;

        if (!$data['novel_name']) {
            $this->error('警告！小说必填项目。');
        }
        //查找小说内容
        $novel_name = $data['novel_name'];
        $chapter_name = $data['chapter_name'];
        $row = D('novel')->field("id,description,img")->where("name = '$novel_name'")->find();
        if (!empty($row)) {
            $data['novel_id'] = $row['id'];
            $data['novel_description'] = $row['description'];
            $data['novel_img'] = $row['img'];
            //添加章节名称
            $novel_id = $row['id'];
            $chapter_row = D('novel_chapter')->field('id')->where("order_set = '$chapter_name' and novel_id = $novel_id")->find();
            if(!empty($chapter_row)){
                $data['chapter_id'] = $chapter_row['id'];
            }

        }
        if ($id) {
            M('today_recommend')->data($data)->where('id=' . $id)->save();
            addlog('编辑推荐，AID：' . $id);
            $this->success('恭喜！编辑成功！');
        } else {
           // echo '新增';
            $aid = M('today_recommend')->data($data)->add();
            if ($aid) {
                addlog('新增推荐，AID：' . $id);
                $this->success('恭喜！新增成功！');
            } else {
                $this->error('抱歉，未知错误！');
            }

        }
    }
}